//
//  BIDimmerViewController.m
//  BIDimmerViewController
//
//  Created by 郑林琴 on 15/11/10.
//  Copyright © 2015年 ET|冰琳. All rights reserved.
//

#import "BIDimmerViewController.h"

@interface BIDimmerViewController ()

@property (nonatomic, strong) UIView *containerView;

@end

@implementation BIDimmerViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self initialization];
    }
    return self;
}

- (instancetype)init{
    if (self = [super init]) {
        [self initialization];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self initialization];
    }
    return self;
}

- (void)initialization{
    
    self.hidesBottomBarWhenPushed = true;
    
    self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 8.0) {
        
        self.modalPresentationStyle = UIModalPresentationCustom;
    }else{
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    self.animateTime = .2f;
    
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat screenWidth  = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat containerHeight = screenHeight / 5 * 2;
    
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight - containerHeight, screenWidth, containerHeight)];
    self.containerView.backgroundColor = [UIColor whiteColor];
    
    self.touchDismiss = YES;
    self.showWithAnimate = true;
}


- (void)loadView{
    [super loadView];
    [self.view addSubview:self.containerView];
    self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
    [self.view addGestureRecognizer:tap];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    if (self.showWithAnimate == NO) {
        return;
    }
    
    CGFloat containerHeight = CGRectGetHeight(self.containerView.frame);
    self.containerView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), containerHeight);
    
    [UIView animateWithDuration:self.animateTime animations:^{
        CGRect frame = CGRectMake(0, CGRectGetHeight(self.view.bounds) - containerHeight, CGRectGetWidth(self.view.bounds), containerHeight);
        self.containerView.frame = frame;
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if (self.showWithAnimate == NO) {
        return;
    }
    CGFloat containerHeight = CGRectGetHeight(self.containerView.frame);
    
    [UIView animateWithDuration:self.animateTime/2.f animations:^{
        
        self.containerView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), containerHeight);
        
    }];
}

#pragma mark - actions
- (void)dismiss:(UITapGestureRecognizer *)tap {
    
    if (self.touchDismiss && !CGRectContainsPoint(self.containerView.frame, [tap locationInView:self.view])) {
        [self dismissViewControllerAnimated:true completion:nil];
    }
}


- (void)dealloc{
    NSLog(@"%s",__func__);
}


- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
    
    if (flag) {
        CGFloat containerHeight = CGRectGetHeight(self.containerView.frame);
        
        [UIView animateWithDuration:self.animateTime/2.f animations:^{
            
            self.containerView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), containerHeight);
            
        } completion:^(BOOL finished) {
            
            CGFloat containerHeight = CGRectGetHeight(self.containerView.frame);
            
            [UIView animateWithDuration:self.animateTime/2.f animations:^{
                
                self.containerView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), containerHeight);
                
            } completion:^(BOOL finished) {
                [super dismissViewControllerAnimated:true completion:nil];
            }];
        }];
    }
    [super dismissViewControllerAnimated:flag completion:completion];
}


@end
